﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class frmbaofeijilu : Form
    {
        static string connectionString = ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        string str = null;
        bool b量具Search, b量具Insert, b量具Edit, b量具Del;
        public frmbaofeijilu( bool b量具Search, bool b量具Insert, bool b量具Edit, bool b量具Del)
        {
            InitializeComponent();
            this.b量具Search = b量具Search;
            this.b量具Insert = b量具Insert;
            this.b量具Edit = b量具Edit;
            this.b量具Del = b量具Del;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tab報廢紀錄_SelectedIndexChanged(object sender, EventArgs e)
        {
            PublicClass.setRowNumber(dataGV報廢紀錄);
            PublicClass.setRowNumber(dataGV報廢人);
            ResetText報廢();
            ResetText報廢人();
        }

        private void frmbaofeijilu_Load(object sender, EventArgs e)
        {
            if (!b量具Insert)
            {
                btnTra.Enabled = false;
                btnAddnewbaofei.Enabled = false;
            }
            if (!b量具Search)
            {
                btnSearch報廢.Enabled = false;
            }
            if (!b量具Edit)
            {
                btnEditbaofei.Enabled = false;
            }
            if (!b量具Del)
            {
                btnDeletebaofei.Enabled = false;
            }
            Load報廢();
            Load報廢人();
            ResetText報廢();
            ResetText報廢人();
        }

        public void Load報廢()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_報廢_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                //dataGV報廢紀錄.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGV報廢紀錄.DataSource = dt;
                //dataGV報廢紀錄.Columns[0].Width = 0;
                PublicClass.setRowNumber(dataGV報廢紀錄);
                ResetText報廢();
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load報廢人()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_報廢人_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGV報廢人.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGV報廢人.DataSource = dt;
                //dataGV報廢人.Columns[0].Width = 0;
                PublicClass.setRowNumber(dataGV報廢人);
                ResetText報廢人();
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void ResetText報廢()
        {
            dateTimePicker_From_報廢日期.ResetText();
            txt量具名稱.ResetText();
            txt量具編號.ResetText();
            txt報廢人.ResetText();
        }

        public void ResetText報廢人()
        {
            txt報廢人代號.ResetText();
            txt報廢人名稱.ResetText();
        }

        private void dataGV報廢人_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV報廢人);
        }

        private void dataGV報廢紀錄_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV報廢紀錄);
        }

        private void btnAddnewbaofei_Click(object sender, EventArgs e)
        {
            if (!b量具Insert)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt報廢人代號.Text == "")
                    {
                        MessageBox.Show("報廢人代號不正確", "錯誤", MessageBoxButtons.OK);
                        txt報廢人代號.Focus();
                        return;
                    }
                    //bat dau insert vao database

                    con.Open();
                    string sp_insert = "sp_報廢人_新增";
                    SqlCommand cmd = new SqlCommand(sp_insert, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@報廢人代號", txt報廢人代號.Text);
                    cmd.Parameters.AddWithValue("@姓名", txt報廢人名稱.Text);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("新增成功");
                    Load報廢人();
                    ResetText報廢人();
                    con.Close();
                    //reset all text
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    Load報廢人();
                    return;
                }
            }
        }

        private void btnEditbaofei_Click(object sender, EventArgs e)
        {
            if (!b量具Edit)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt報廢人代號.Text == "") { MessageBox.Show("請您輸入報廢人代號更新"); }
                    else
                    {
                        con.Open();
                        SqlCommand cmd2;
                        String sql = "SELECT COUNT(*) FROM 報廢人 WHERE 報廢人代號=N'" + str + "'";
                        cmd2 = new SqlCommand(sql, con);
                        if ((int)cmd2.ExecuteScalar() > 0)
                        {
                            string sp_update = "sp_報廢人_修改";
                            cmd2 = new SqlCommand(sp_update, con);
                            cmd2.CommandType = CommandType.StoredProcedure;
                            cmd2.Parameters.AddWithValue("@報廢人代號", str);
                            cmd2.Parameters.AddWithValue("@報廢人代號2", txt報廢人代號.Text);
                            cmd2.Parameters.AddWithValue("@姓名", txt報廢人名稱.Text);
                            cmd2.ExecuteNonQuery();
                            MessageBox.Show("更新成功");
                            Load報廢人();
                            ResetText報廢人();
                            con.Close();
                            return;
                        }
                        else
                        {
                            MessageBox.Show("Error! '" + txt報廢人代號.Text.ToString() + "' 不由來");
                            con.Close();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    Load報廢人();
                    return;
                }
            }
        }

        private void btnDeletebaofei_Click(object sender, EventArgs e)
        {
            if (!b量具Del)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt報廢人代號.Text == "") { MessageBox.Show("請您輸入報廢人代號刪除"); }
                    else
                    {

                        DialogResult answer = MessageBox.Show("您確定要出入報廢人代號碼", "警報", MessageBoxButtons.YesNo);
                        if (answer == DialogResult.Yes)
                        {
                            con.Open();
                            string sp_delete = "sp_報廢人_刪除";
                            SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                            sqldelete.CommandType = CommandType.StoredProcedure;
                            sqldelete.Parameters.AddWithValue("@報廢人代號", txt報廢人代號.Text);
                            MessageBox.Show("刪除成功");
                            sqldelete.ExecuteNonQuery();
                            Load報廢人();
                            con.Close();
                            ResetText報廢人();
                            return;
                        }
                        else
                        {
                            Load報廢人();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    Load報廢人();
                    return;
                }
            }
        }

        private void btnResetbaofei_Click(object sender, EventArgs e)
        {            
            ResetText報廢人();
            Load報廢人();
        }

        private void dataGV報廢人_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int r = e.RowIndex;
                str = dataGV報廢人.Rows[r].Cells[0].Value.ToString();
                txt報廢人代號.Text = dataGV報廢人.Rows[r].Cells[0].Value.ToString();
                txt報廢人名稱.Text = dataGV報廢人.Rows[r].Cells[1].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void txt報廢人_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 報廢人 where 報廢人代號=N'{0}'", txt報廢人.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName報廢人.Text = "";
                else lbName報廢人.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            ResetText報廢();
            Load報廢();
        }

        //string tbf0, tbf1, tbf2, tbf3, tbf4, tbf5, tbf6, tbf7;
        //string ptf1, ptf2, ptf3, ptf4, ptf5;
        string tbx0, tbx1, tbx2, tbx3, tbx4, tbx5;
        string xiuli;
        private void btnTra_Click(object sender, EventArgs e)
        {
            try { 
            
            xiuli = "3";
            int r = dataGV報廢紀錄.CurrentCell.RowIndex;
            string id0 = dataGV報廢紀錄.Rows[r].Cells[0].Value.ToString();
            string id1 = dataGV報廢紀錄.Rows[r].Cells[1].Value.ToString();
            string id6 = dataGV報廢紀錄.Rows[r].Cells[2].Value.ToString();
            string id2 = dataGV報廢紀錄.Rows[r].Cells[4].Value.ToString();
            string id3 = dataGV報廢紀錄.Rows[r].Cells[5].Value.ToString();
            string id4 = dataGV報廢紀錄.Rows[r].Cells[3].Value.ToString();
            string id5 = dataGV報廢紀錄.Rows[r].Cells[6].Value.ToString();

            tbx0 = id0;
            tbx1 = id1; //tb1 0
            tbx2 = id2; //cb1 1
            tbx3 = id3; //tb2 2
            tbx4 = id4; //tb3 3
            tbx5 = id5; //cb2 4

            frmXiuLi frm = new frmXiuLi(xiuli, tbx0, tbx1, tbx2, tbx3, tbx4, tbx5, null, null, null, null, null, null, null, null, null, b量具Search, b量具Insert, b量具Edit, b量具Del);
            frm.ShowDialog();
            Load報廢();
            }catch(Exception ex){
                MessageBox.Show(ex.Message); 
            }
        }

        private void btnSearch報廢_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                //if (Ck領用日期.Checked == true)
                //{

                //    //領用紀錄load();
                //}
                //if (Ck歸還日期.Checked == true)
                //{

                //    //領用紀錄load();
                //}
                con.Open();
                string sp = "sp_報廢_尋找";
                SqlCommand cmd = new SqlCommand(sp, con);
                cmd.CommandType = CommandType.StoredProcedure;
                if (txt量具編號.Text != "") cmd.Parameters.AddWithValue("@量具編號", txt量具編號.Text);
                if (txt量具名稱.Text.ToString() != "") cmd.Parameters.AddWithValue("@量具名稱", txt量具名稱.Text);
                //if (dateTimePicker_From_報廢日期.Text != "") cmd.Parameters.AddWithValue("@報廢日期", DateTime.Parse(dateTimePicker_From_報廢日期.Text));
                //if (dateTimePicker_From_修理日期.Text != "") cmd.Parameters.AddWithValue("@修理日期", DateTime.Parse(dateTimePicker_From_修理日期.Text));
                if (txt報廢人.Text != "") cmd.Parameters.AddWithValue("@報廢人代號", txt報廢人.Text);
                cmd.Parameters.AddWithValue("@datefrom", dateTimePicker_From_報廢日期.Value);
                cmd.Parameters.AddWithValue("@todate", todate.Value);
                //if (txt受理人.Text.ToString() != "") cmd.Parameters.AddWithValue("@受理人代號", txt受理人.Text);
                SqlDataReader sqlreader = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                //dt.Columns.Add("項次");//add new 項次 column
                dt.Columns.Add("量具編號");
                dt.Columns.Add("量具狀態");
                dt.Columns.Add("量具名稱");
                dt.Columns.Add("規格型號");
                dt.Columns.Add("存放地點");
                dt.Columns.Add("測量範圍");
                dt.Columns.Add("數量");
                dt.Columns.Add("報廢日期");
                dt.Columns.Add("報廢原因");
                dt.Columns.Add("報廢人代號");
                dt.Columns.Add("備註");
                //int k=1;
                while (sqlreader.Read())
                {
                    DataRow dtrows = dt.NewRow();
                    //dtrows["項次"]=k.ToString();
                    dtrows["量具編號"] = sqlreader["量具編號"];
                    dtrows["量具狀態"] = sqlreader["量具狀態"];
                    dtrows["量具名稱"] = sqlreader["量具名稱"];
                    dtrows["規格型號"] = sqlreader["規格型號"];
                    dtrows["存放地點"] = sqlreader["存放地點"];
                    dtrows["測量範圍"] = sqlreader["測量範圍"];
                    dtrows["數量"] = sqlreader["數量"];
                    dtrows["報廢日期"] = sqlreader["報廢日期"];
                    dtrows["報廢原因"] = sqlreader["報廢原因"];
                    dtrows["報廢人代號"] = sqlreader["報廢人代號"];
                    dtrows["備註"] = sqlreader["備註"];
                    dt.Rows.Add(dtrows);
                    //k++;
                }
                dataGV報廢紀錄.DataSource = dt;
                PublicClass.setRowNumber(dataGV報廢紀錄);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void btn報廢人Search_Click(object sender, EventArgs e)
        {
            frmSearchBaofeiRen frm = new frmSearchBaofeiRen(txt報廢人);
            frm.ShowDialog();
        }
    }
}
